package com.cxyd.modules.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cxyd.common.page.PageData;
import com.cxyd.common.service.impl.CrudServiceImpl;
import com.cxyd.modules.sys.service.SysUserService;
import com.cxyd.modules.wms.dao.TemporaryContDao;
import com.cxyd.modules.wms.dto.TemporaryContDTO;
import com.cxyd.modules.wms.entity.TemporaryContEntity;
import com.cxyd.modules.wms.service.TemporaryContService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 临时托盘信息
 *
 * @author g_Cycle gengruyi01@163.com
 * @since 1.0.0 2023-04-27
 */
@Service
public class TemporaryContServiceImpl extends CrudServiceImpl<TemporaryContDao, TemporaryContEntity, TemporaryContDTO> implements TemporaryContService {
    private final SysUserService userService;

    public TemporaryContServiceImpl(SysUserService userService) {
        this.userService = userService;
    }

    @Override
    public PageData<TemporaryContDTO> page(Map<String, Object> params) {
        //1.查询物料信息
        final IPage<TemporaryContEntity> page = baseDao.selectViewPage(
                getPage(params, "id", true),
                getWrapper(params)
        );

        return getPageData(page, TemporaryContDTO.class);
    }

    @Override
    public QueryWrapper<TemporaryContEntity> getWrapper(Map<String, Object> params) {
        //查询条件
        String contNo = (String) params.get("contNo");

        QueryWrapper<TemporaryContEntity> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(contNo), "s1.cont_no", contNo);
        return wrapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(@RequestBody Long[] ids) {
        for (int i = 0; i < ids.length; i++) {
            deleteByLocateId(ids[i]);
        }
    }

    public void deleteByLocateId(Long id) {
        baseDao.deleteById(id);
    }
}